import axios from 'axios';
import FormData from 'form-data';
import log from 'electron-log';
// import iconv from 'iconv-lite';

class DamiHelper {
    private username = '280059236';//'a123';
    private password = 'Qq280059236';//'a123';
    private projectId = '72142';
    private token = '';
    async login(projectId: string, name: string, password: string) {
        this.projectId = projectId;
        this.username = name;
        this.password = password;
        const formData = new FormData();
        formData.append('username', this.username);
        formData.append('password', this.password);
        try {
            const res = await getToken(formData);
            // console.log(res);
            if (res.data.flag && res.data.code === 20000 ) {
                console.log(res.data)
                log.info("【订单】：" + JSON.stringify(res.data));
                this.token = res.data.data.usertoken;
            } else {
                throw new Error(res.data.msg);
            }
        } catch (error: any) {
            throw new Error(error.message);
        }
    }

    isLogin() {
        return !!this.token;
    }

    async getOrderInfo(orderId: string) {
        const formData = new FormData();
        formData.append('token', this.token);
        formData.append('orderId', orderId);
        try {
            const res = await getOrderInfo2(formData);
            // console.log(res);
            return res.data;
        } catch (error) {
            
        }
    }

    async createTask(qrCode: string) {
        // 'https://open.weixin.qq.com/connect/confirm?uuid=061OHIuk0iGC000R'
        const formData = new FormData();
        formData.append('token', this.token);
        formData.append('appId', this.projectId);
        formData.append('qrCode', qrCode);
        // formData.append('orderId', '1772470686229954560');
        // console.log(qrCode)
        const res = await createTaskApi(formData);
        if (!res.data.flag || res.data.code !== 20000 ) {
            throw new Error(res.data.message);
            // throw new Error(iconv.encode(res.data.message, 'gbk').toString());
        }
        console.log(res.data)
    }
}

const getToken = async (formData: FormData) => {
    return axios.post("http://dami.ifzbs.cn:8881/public/userLogin", formData, {
        // responseType: 'arraybuffer',
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

export const getOrderInfo = (formData: FormData) => {
    return axios.post(`http://dami.ifzbs.cn:8881/public/getorderinfo`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}

export const getOrderInfo2 = (formData: FormData) => {
    // http://dami.ifzbs.cn:8884/public/getauthcode
    // getresult
    return axios.post(`http://dami.ifzbs.cn:8884/public/getresult`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}


const createTaskApi = async (formData: FormData) => {
    return axios.post("http://dami.ifzbs.cn:8881/createTaskApi", formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}

export default DamiHelper;